package gdr.dev.creator;

import gdr.action.descriptor.*;
import gdr.battle.core.BattleContainer;
import gdr.battle.core.Client;
import gdr.battle.data.map.Position;
import gdr.battle.graphics.CoordinateCalculator;
import gdr.battle.graphics.GraphicalPlayer;
import gdr.battle.graphics.HumanGraphicalClient;
import gdr.dev.SuperGDR9000;
import gdr.player.DefaultPlayer;
import gdr.player.Team;

import java.util.*;

public class OnMapPlayerCreator {
	
	public static DefaultPlayer createGraphicalPlayer(SuperGDR9000 gdr, BattleContainer bc, boolean isOnMap, Area area, HumanGraphicalClient humanClient, Client client, CoordinateCalculator coordinate, Team team, DefaultPlayer player){
		GraphicalPlayer gp = new GraphicalPlayer();
		gp.setClient(humanClient);
		gp.setCoordinate(coordinate);
		gp.setPlayer(player);
		gp.getPlayer().setTeam(team);
		client.getClientTeam().getPlayers().add(gp.getPlayer());
		gp.getPlayer().setGraphical(gp);
		gp.setSprite(gp.getPlayer().getClasse().cloneSprite());
		
		if (isOnMap){
			Position pos = area.getFreePos(bc, player);
			if (pos != null){
				gp.getPlayer().setOnMap(true);
				gp.getPlayer().setPosizione(pos);
				bc.getData().getMap().getBattleMapUnit(pos).getPlayer().add(gp.getPlayer());
			}
		}

		// cerco anche tra le azioni, quelle che riguardano l'evocazione di un personaggio devono essere trattate.
		ArrayList<Azione> azioni = gp.getPlayer().getClasse().getAllActions();
		for (int i=0; i<azioni.size(); i++)
			if (azioni.get(i) instanceof Summon){
				DefaultPlayer summon = ((Summon)azioni.get(i)).getSummoned();
				createGraphicalPlayer(gdr, bc, false, area, humanClient, client, coordinate, team, summon);
			}
		return gp.getPlayer();
	}
}
